<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册 - 面对面建群</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.1/font/bootstrap-icons.css">
</head>
<body class="bg-light">
<div id="app" class="container">
    <div class="row justify-content-center mt-5">
        <div class="col-md-6">
            <div class="card shadow">
                <div class="card-header bg-primary text-white">
                    <h4 class="mb-0">注册账号</h4>
                </div>
                <div class="card-body">
                    <form @submit.prevent="register">
                        <div class="mb-3">
                            <label for="username" class="form-label">用户名</label>
                            <input v-model="formData.username" type="text" class="form-control" id="username" required>
                            <div class="form-text">用户名将用于登录，不可更改</div>
                        </div>
                        <div class="mb-3">
                            <label for="password" class="form-label">密码</label>
                            <input v-model="formData.password" type="password" class="form-control" id="password" required>
                        </div>
                        <div class="mb-3">
                            <label for="confirmPassword" class="form-label">确认密码</label>
                            <input v-model="confirmPassword" type="password" class="form-control" id="confirmPassword" required>
                            <div v-if="passwordMismatch" class="text-danger">两次输入的密码不一致</div>
                        </div>
                        <div class="mb-3">
                            <label for="nickname" class="form-label">昵称</label>
                            <input v-model="formData.nickname" type="text" class="form-control" id="nickname" required>
                            <div class="form-text">昵称将显示给其他用户</div>
                        </div>
                        <div class="mb-3">
                            <label for="avatar" class="form-label">头像URL (可选)</label>
                            <input v-model="formData.avatar" type="text" class="form-control" id="avatar">
                            <div class="form-text">输入头像图片的URL地址</div>
                        </div>
                        <div class="d-grid gap-2">
                            <button type="submit" class="btn btn-primary" :disabled="loading">
                                <span v-if="loading" class="spinner-border spinner-border-sm me-1" role="status" aria-hidden="true"></span>
                                注册
                            </button>
                            <button type="button" class="btn btn-outline-secondary" @click="goToLogin">返回登录</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/vue@3.2.37/dist/vue.global.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
    const { createApp, ref, computed, watch } = Vue;

    createApp({
        setup() {
            const formData = ref({
                username: '',
                password: '',
                nickname: '',
                avatar: ''
            });
            const confirmPassword = ref('');
            const loading = ref(false);

            const passwordMismatch = computed(() => {
                return confirmPassword.value && formData.value.password !== confirmPassword.value;
            });

            watch(confirmPassword, () => {
                // 当确认密码改变时，重新计算是否匹配
            });

            const register = async () => {
                // 验证密码是否匹配
                if (formData.value.password !== confirmPassword.value) {
                    return;
                }

                loading.value = true;
                try {
                    const response = await axios.post('/api/auth/register', formData.value);

                    if (response.data.code === 200) {
                        alert('注册成功，请登录');
                        window.location.href = '/login.html';
                    } else {
                        alert(response.data.message || '注册失败');
                    }
                } catch (error) {
                    console.error('Registration failed:', error);
                    alert('注册失败: ' + (error.response?.data?.message || error.message));
                } finally {
                    loading.value = false;
                }
            };

            const goToLogin = () => {
                window.location.href = '/login.html';
            };

            return {
                formData,
                confirmPassword,
                loading,
                passwordMismatch,
                register,
                goToLogin
            };
        }
    }).mount('#app');
</script>
</body>
</html>